package com.itboye.hutouben.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.bean.TopicListlBean;
import com.itboye.hutouben.util.GlideLoader;
import com.itboye.hutouben.widget.NineGridTestLayout;

import java.util.List;

/***
 * 论坛首页
 */
public class LunTanPageIndexAdapter extends android.widget.BaseAdapter implements View.OnClickListener {
    List<TopicListlBean.TopicList> datas;
    int position;
    Activity context;
    ClickListener ClickListener;

    public LunTanPageIndexAdapter(Activity context, List<TopicListlBean.TopicList> datas, ClickListener ClickListener) {
        this.context = context;
        this.datas = datas;
        this.ClickListener = ClickListener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_luntan_index, null, false);
            holder.img_name = (TextView) convertView.findViewById(R.id.img_name);
            holder.img_head = (ImageView) convertView.findViewById(R.id.img_head);
            holder.dianzan_img = (ImageView) convertView.findViewById(R.id.dianzan_img);
            holder.txt_content = (TextView) convertView.findViewById(R.id.txt_content);
            holder.txt_title = (TextView) convertView.findViewById(R.id.txt_title);
            holder.layout_nine_grid = (NineGridTestLayout) convertView.findViewById(R.id.layout_nine_grid);
            holder.chlid_title = (TextView) convertView.findViewById(R.id.chlid_title);
            holder.chlid_name = (TextView) convertView.findViewById(R.id.chlid_name);
            holder.chlid_content = (TextView) convertView.findViewById(R.id.chlid_content);
            holder.chlid_nine_grid = (NineGridTestLayout) convertView.findViewById(R.id.chlid_nine_grid);
            holder.layout_chlid = (LinearLayout) convertView.findViewById(R.id.layout_chlid);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.dianzan = (TextView) convertView.findViewById(R.id.dianzan);
            holder.pinglun = (TextView) convertView.findViewById(R.id.pinglun);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        GlideLoader.displayImageHeadCircular(context, datas.get(position).getUid() + "", holder.img_head);

        //  XImageLoader.load(Const.IMAGE_HEAD_TWO + datas.get(position).getUid()+"", holder.img_head);
        holder.img_name.setText(datas.get(position).getUname());

        holder.txt_content.setText(datas.get(position).getContent());
        if (datas.get(position).getImg() != null) {
            if (datas.get(position).getImg().size() > 0) {
                holder.layout_nine_grid.removeAllViews();
                holder.layout_nine_grid.setUrlList(datas.get(position).getImg());

            } else {
                holder.layout_nine_grid.removeAllViews();
                holder.layout_nine_grid.setVisibility(View.GONE);
            }
        } else {
            holder.layout_nine_grid.removeAllViews();
            holder.layout_nine_grid.setVisibility(View.GONE);
        }

        holder.time.setText(datas.get(position).getCreate_time_desc());
        holder.dianzan.setText(datas.get(position).getLikes() + "");
        holder.pinglun.setText(datas.get(position).getReplys_direct_count() + "");

        if (datas.get(position).getHas_like() != 0) {
            holder.dianzan_img.setImageResource(R.drawable.dianzan_two);
        } else {
            holder.dianzan_img.setImageResource(R.drawable.dianzai_one);
        }
        holder.dianzan_img.setOnClickListener(this);
        holder.dianzan_img.setTag(position);
      //  holder.pinglun.setOnClickListener(this);
      //  holder.pinglun.setTag(position);

        //子内容
        if (datas.get(position).getRepeat_count() != 0) {
            holder.txt_title.setVisibility(View.GONE);
            holder.layout_chlid.setVisibility(View.VISIBLE);
            holder.chlid_title.setText(datas.get(position).getRepeat_info().getUname());
            holder.chlid_name.setText(":\"" + datas.get(position).getRepeat_info().getTitle() + "\"" );
            holder.chlid_content.setText(datas.get(position).getRepeat_info().getContent());
            if (datas.get(position).getRepeat_info().getImg() != null) {
                if (datas.get(position).getRepeat_info().getImg().size() > 0) {

//                    if (!datas.get(position).getRepeat_info().getImg().equals(holder.chlid_nine_grid.getTag())) {
//                        holder.chlid_nine_grid.setTag(datas.get(position).getRepeat_info().getImg());
//                        holder.chlid_nine_grid.removeAllViews();
//                        holder.chlid_nine_grid.setUrlList(datas.get(position).getRepeat_info().getImg());
//                    }
                    holder.chlid_nine_grid.removeAllViews();
                    holder.chlid_nine_grid.setUrlList(datas.get(position).getRepeat_info().getImg());
                } else {
                    holder.chlid_nine_grid.removeAllViews();
                    holder.chlid_nine_grid.setVisibility(View.GONE);
                }
            } else {
                holder.chlid_nine_grid.removeAllViews();
                holder.chlid_nine_grid.setVisibility(View.GONE);
            }
        } else {
            holder.layout_chlid.setVisibility(View.GONE);
            holder.txt_title.setVisibility(View.VISIBLE);
            holder.txt_title.setText(datas.get(position).getTitle());
        }

        return convertView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dianzan_img:
                ClickListener.dianzan(v);
                break;
//            case R.id.pinglun:
//                ClickListener.pinglun(v);
//                break;
        }
    }

    public interface ClickListener {
        public void dianzan(View v);//点赞

        public void pinglun(View v);//评论
    }

    public static class ViewHolder {
        TextView img_name, txt_title, txt_content, chlid_title,chlid_name, chlid_content;
        ImageView img_head,dianzan_img;
        NineGridTestLayout layout_nine_grid, chlid_nine_grid;
        LinearLayout layout_chlid;
        TextView time, dianzan, pinglun;
    }

}
